package com.fintech.third.utils;

import java.util.Date;
import java.util.Iterator;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;

import com.fintech.third.dao.SmsTemplateMapper;
import com.fintech.third.dao.sms.SmsLogMapper;
import com.fintech.third.model.message.SmsTemplate;
import com.fintech.third.model.sms.SmsLog;
import com.fintech.third.service.SmsDataDeal;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;

import net.sf.json.JSONObject;

public class SmsSendThread implements Runnable {

	private SmsTemplateMapper smsTemplateMapper=(SmsTemplateMapper)ApplicationContextGet.getApplicationContext().getBean("smsTemplateMapper");

	private SmsLogMapper smsLogMapper=(SmsLogMapper)ApplicationContextGet.getApplicationContext().getBean("smsLogMapper");
	
	private SmsDataDeal smsDataDeal=(SmsDataDeal)ApplicationContextGet.getApplicationContext().getBean("smsDataDeal");
	
	private  Environment env =(Environment) ApplicationContextGet.getApplicationContext().getBean(Environment.class);
	
	private Object object;

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	@Override
	public void run() {
		
		JSONObject json = JSONObject.fromObject(object);
		String phone = json.get("phone").toString();
		MarvelsLogUtil.getInstance().info("=========>>>>发送短信线程开始,手机号:"+phone);
		String templateId = json.get("template_id").toString();
		String src = json.get("src").toString();
		String createTime = json.get("create_time").toString();
		SmsTemplate smsTemplate = smsTemplateMapper.obtainSmsTemplateContent(Long.parseLong(templateId));
		String templateContent = smsTemplate.getContent_template_document();
		
		Integer number = smsDataDeal.querySendSuccussCount(phone, templateId);
		String configPropertiesPath = env.getProperty("config.properties.path");
		String maxNumberStr  = ServerProperties.getValue(configPropertiesPath, "common.properties", "sms.template.send.max.number");
		Integer maxNumber = maxNumberStr == null ? 5 : Integer.parseInt(maxNumberStr);
		if(number >= maxNumber){
			MarvelsLogUtil.getInstance().info(String.format("手机号【%s】,模板id【%s】,超过允许发送最大次数【%d次】,拒绝发送", phone, templateId, maxNumber));
			smsDataDeal.dealSmsSendFailure(createTime, templateId, src, templateContent, phone, "", "", "", "", "", 
					String.format("当日发送量超过允许发送最大次数【%d次】,拒绝发送！", maxNumber));
		}else{
			Iterator ite = json.keys();
			while (ite.hasNext()) {
				String  key = (String) ite.next();
				String  value = json.getString(key);
				templateContent = templateContent.replace(key, value);
			}
			smsDataDeal.smsDealAndSend(createTime, templateId, src, templateContent, phone,"","","","");
			MarvelsLogUtil.getInstance().info("=========>>>>发送短信线程结束,手机号:"+phone);
			try {
				//写入sms_log记录   用于计费统计
				SmsLog smsLog=new SmsLog();
				smsLog.setPhone(phone);
				smsLog.setContent(templateContent);
				smsLog.setSendTime(new Date());
				smsLog.setStatus(true);
				smsLogMapper.insertSelective(smsLog);
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error(">>>>定时发短信写入短信统计表异常:",e);
			}
		}
	}
	
}
